<?php
namespace app\admin\controller;
use think\Controller;
use think\facade\Db;
use think\facade\View;
use think\Log;
use app\admin\controller\Common;
use app\admin\controller\Room;
use app\service\roomService;
use app\service\lockService;
use app\service\wotuService;
use app\service\personService;
use app\service\sendmsgService;
use app\service\iotmcuService;
use app\service\deviceService;
class Personnel extends Common{

	public function ufacelist()
    {
		 //查询小区信息
        $com = $this->pidTokenId();
        //$whe['id'] = array('in',$com['community_id']);
        $whe=[];
        $whe[] =["id","in",$com['community_id']];
        $communitylist = (new roomService())->communityList($whe);
        //Log::write($communitylist,"community");
        //var_dump($communitylist);exit;
        $community_id = $com['community_id'];
        View::assign('communitylist',$communitylist);
        //查询
        $data = request()->get();
        // dump($data);exit();
        if (!empty($data['community_id'])){
            $groups = (new roomService())->disgroup($data['community_id']);
            View::assign('groups', $groups);
            // dump($data['group']);
            if (!empty($data['group'])) {
                $buildings = (new roomService())->disbuilding($data['community_id'],$data['group']);
                View::assign('buildings', $buildings);
                // dump($buildings);exit();
                if (!empty($data['building'])) {
                    $units =  (new roomService())->disunit($data['community_id'], $data['group'],$data['building']);
                    View::assign('units', $units);
                    if(!empty($data['unit'])){
                        $rooms = (new roomService())->disroom($data['community_id'], $data['group'],$data['building'],$data['unit']);
                        View::assign('rooms',$rooms);
                    }
                }
            }
        }
        
        $where = [];
        if(!empty($data)){
            if(!empty($data['community_id'])){
                $where['community_id'] = $data['community_id'];//小区名称
            }
            if(!empty($data['group'])){
                $where['group'] = $data['group'];//组团
            }
            if(!empty($data['building'])){
                $where['building'] = $data['building'];
            }
            if(!empty($data['unit'])){
                $where['unit'] = $data['unit'];
            }
            if(!empty($data['room'])){
                $where['room'] = $data['room'];
            }
            if(!empty($data['owner_name'])){
                $where['name'] = array("like","%".$data['owner_name']."%");
            }
            if(!empty($data['phone'])){
                $where['phone'] = array("like","%".$data['phone']."%");
            }
            
        }
        //die;
        $where = array_filter($where);
        $where["relation"]=0;
        // dump($whereroom);dump($where);exit();
        $page = request()->has('page') ? request()->get('page') : 1;
        //查询房屋条数
        $number = (new roomService())->personnelListNumber($where,$community_id);
        View::assign('number',$number);
        //查询房屋信息
        $personnellist = (new roomService())->personnelList($page,$where,$community_id);
       
        foreach($personnellist as $userinfo){
            $userinfo['room_id'] = $userinfo['building'].'幢'.$userinfo['unit'].'单元'.$userinfo['room'].'室';

            if($userinfo["personGuid"]=="0")
            {
                $cardNo=$userinfo["room"].substr($userinfo["room"],0,2);
                $type=$type=$userinfo["community_name"]."-".$userinfo["address"];
                $re=(new wotuService())->reg($userinfo["name"],$userinfo["phone"],$cardNo,$userinfo["idcard"],$type);

                $result=json_decode($re,true);
                $guid=$result["data"]["guid"];
                Db::table("personnel")->where("id",$userinfo["id"])->update(["personGuid"=>$guid]);
                $userinfo["personGuid"]=$guid;
            }
            //获取uface中的图片
            $res=(new wotuService())->personGuid($userinfo["personGuid"]);
            //echo "<pre>";var_dump($res);die;
            $userinfo["pic"]=$res["data"]["faces"];
            $userinfo["picnum"]=count($userinfo["pic"]);
            $list[] = $userinfo;
        }
        if(!isset($list)){
            View::assign('list',$personnellist);
        }else{
            View::assign('list',$list);
        }
        View::assign('personnellist',$personnellist);
       
        foreach($data as $k => $v) {
            View::assign($k, $v);
        }
        return View::fetch('uface_list');
	}
    public function personnelAdd(){
         //查询小区信息
        $com = $this->pidTokenId();
        $whe['id'] = array('in',$com['community_id']);
        $communitylist = (new roomService())->communityList($whe);
        $community_id = $com['community_id'];
        View::assign('communitylist',$communitylist);
        
        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();
            $communityinfo=Db::table("community")->where("id",$data["community_id"])->find();
            
            $roominfo = (new roomService())->roominfoName($data["community_id"],$data["group"],$data["building"],$data["unit"],$data["room"]);
            $data['room_id'] = $roominfo['id'];
            $data["address"]=$data["group"].$data["building"]."幢".$data["unit"]."单元".$data["room"]."室";
            $data['community_name'] = $communityinfo["name"];
            $data['xmcode'] = $communityinfo["xmcode"];
            //添加时间
            $bill['create_time'] =time();
            //数据插入到数据库
            $result = Db::table("personnel")->insert($data);
            //判断数据是否插入成功
            if($result){
                $this->success('添加成功',url('ufacelist'));
            }else{
                $this->error('添加失败',url('ufacelist'));
            }
        }else{
           return View::fetch('personnel_add'); 
        }
    }


    
    public function familylist()
    {
         //查询小区信息
        $com = $this->pidTokenId();
        //$whe['id'] = array('in',$com['community_id']);
        $whe=[];
        $whe[] =["id","in",$com['community_id']];
        $communitylist = (new roomService())->communityList($whe);
        //Log::write($communitylist,"community");
        $community_id = $com['community_id'];
        View::assign('communitylist',$communitylist);
        //查询
        $data = request()->get();
        // dump($data);exit();
        if (!empty($data['community_id'])){
            $groups = (new roomService())->disgroup($data['community_id']);
            View::assign('groups', $groups);
            // dump($data['group']);
            if (!empty($data['group'])) {
                $buildings = (new roomService())->disbuilding($data['community_id'],$data['group']);
                View::assign('buildings', $buildings);
                // dump($buildings);exit();
                if (!empty($data['building'])) {
                    $units =  (new roomService())->disunit($data['community_id'], $data['group'],$data['building']);
                    View::assign('units', $units);
                    if(!empty($data['unit'])){
                        $rooms = (new roomService())->disroom($data['community_id'], $data['group'],$data['building'],$data['unit']);
                        View::assign('rooms',$rooms);
                    }
                }
            }
        }
        
        $where = [];
        if(!empty($data)){
            if(!empty($data['community_id'])){
                $where['community_id'] = $data['community_id'];//小区名称
            }else{
                $where['community_id'] = "";
            }
            if(!empty($data['group'])){
                $where['group'] = $data['group'];//组团
            }else{
                $where['group'] = "";
            }
            if(!empty($data['building'])){
                $where['building'] = $data['building'];
            }else{
                $where['building'] = "";
            }
            if(!empty($data['unit'])){
                $where['unit'] = $data['unit'];
            }else{
                $where['unit'] = "";
            }
            if(!empty($data['room'])){
                $where['room'] = $data['room'];
            }else{
                $where['room'] = "";
            }
            if(!empty($data['owner_name'])){
                $where['name'] = array("like","%".$data['owner_name']."%");
            }else{
                $where['name'] = "";
            }
            if(!empty($data['phone'])){
                $where['phone'] = array("like","%".$data['phone']."%");
            }else{
                $where['phone'] = "";
            }
            
        }
        //die;
       
        
        $where = array_filter($where);
        $where["relation"]=1;
        // dump($whereroom);dump($where);exit();
        $page = request()->has('page') ? request()->get('page') : 1;
        //查询房屋条数
        $number = (new roomService())->personnelListNumber($where,$community_id);
        View::assign('number',$number);
        //查询房屋信息
        $personnellist = (new roomService())->personnelList($page,$where,$community_id);
        
        foreach($personnellist as $userinfo){
            $userinfo['room_id'] = $userinfo['building'].'幢'.$userinfo['unit'].'单元'.$userinfo['room'].'室';
           

            if($userinfo["personGuid"]=="0"||empty($userinfo["personGuid"]))
            {


                $cardNo=$userinfo["room"].substr($userinfo["room"],0,2);
                $type=$userinfo["community_name"]."-".$userinfo["address"];
                $re=(new wotuService())->reg($userinfo["name"],$userinfo["phone"],$cardNo,$userinfo["idcard"],$type);

                $result=json_decode($re,true);
                $guid=$result["data"]["guid"];
                Db::table("personnel")->where("id",$userinfo["id"])->update(["personGuid"=>$guid]);
                $userinfo["personGuid"]=$guid;
            }
            //获取uface中的图片
            $res=(new wotuService())->personGuid($userinfo["personGuid"]);
            $userinfo["pic"]=array();
            if(isset($res["data"]["faces"])){
                $userinfo["pic"]=$res["data"]["faces"];
            }

            $userinfo["picnum"]=count($userinfo["pic"]);
            $list[] = $userinfo;
        }
        
        if(!isset($list)){
            View::assign('list',$personnellist);
        }else{
            View::assign('list',$list);
        }
        View::assign('personnellist',$personnellist);
       
        foreach($data as $k => $v) {
            View::assign($k, $v);
        }
        return View::fetch('family_list');
    }
    //公司企业员工添加
    public function familyadd(){
          //查询小区信息
        $com = $this->pidTokenId();
        //$whe['id'] = array('in',$com['community_id']);
        $whe=[];
        $whe[] =["id","in",$com['community_id']];
        $communitylist = (new roomService())->communityList($whe);
        $community_id = $com['community_id'];
        View::assign('communitylist',$communitylist);
        
        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();
            if(!$this->checkPhoneNumber($data["phone"])){
                $this->error('手机号码错误',url('familyadd'));
            }

            $communityinfo=Db::table("community")->where("id",$data["community_id"])->find();
            
            $roominfo = (new roomService())->roominfoName($data["community_id"],$data["group"],$data["building"],$data["unit"],$data["room"]);
            $data['room_id'] = $roominfo['id'];
             $data["address"]=$data["group"].$data["building"]."幢".$data["unit"]."单元".$data["room"]."室";
            $data['community_name'] = $communityinfo["name"];
            $data['xmcode'] = $communityinfo["xmcode"];
            //添加时间
            $bill['create_time'] =time();
            //数据插入到数据库
            $result = Db::table("personnel")->insert($data);
            //判断数据是否插入成功
            if($result){
                $this->success('添加成功',url('familylist'));
            }else{
                $this->error('添加失败',url('familylist'));
            }
        }else{
           return View::fetch('family_add'); 
        }
    }

    public function ternetlist()
    {
        //查询小区信息
        $com = $this->pidTokenId();
        //$whe['id'] = array('in',$com['community_id']);
        $whe=[];
        $whe[] =["id","in",$com['community_id']];
        $communitylist = (new roomService())->communityList($whe);
        //Log::write($communitylist,"community");
        $community_id = $com['community_id'];
        View::assign('communitylist',$communitylist);
        //查询
        $data = request()->get();
        // dump($data);exit();
        if (!empty($data['community_id'])){
            $groups = (new roomService())->disgroup($data['community_id']);
            View::assign('groups', $groups);
            // dump($data['group']);
            if (!empty($data['group'])) {
                $buildings = (new roomService())->disbuilding($data['community_id'],$data['group']);
                View::assign('buildings', $buildings);
                // dump($buildings);exit();
                if (!empty($data['building'])) {
                    $units =  (new roomService())->disunit($data['community_id'], $data['group'],$data['building']);
                    View::assign('units', $units);
                    if(!empty($data['unit'])){
                        $rooms = (new roomService())->disroom($data['community_id'], $data['group'],$data['building'],$data['unit']);
                        View::assign('rooms',$rooms);
                    }
                }
            }
        }

        $where = [];
        if(!empty($data)){
            if(!empty($data['community_id'])){
                $where['community_id'] = $data['community_id'];//小区名称
            }else{
                $where['community_id'] = "";
            }
            if(!empty($data['group'])){
                $where['group'] = $data['group'];//组团
            }else{
                $where['group'] = "";
            }
            if(!empty($data['building'])){
                $where['building'] = $data['building'];
            }else{
                $where['building'] = "";
            }
            if(!empty($data['unit'])){
                $where['unit'] = $data['unit'];
            }else{
                $where['unit'] = "";
            }
            if(!empty($data['room'])){
                $where['room'] = $data['room'];
            }else{
                $where['room'] = "";
            }
            if(!empty($data['owner_name'])){
                $where['name'] = array("like","%".$data['owner_name']."%");
            }else{
                $where['name'] = "";
            }
            if(!empty($data['phone'])){
                $where['phone'] = array("like","%".$data['phone']."%");
            }else{
                $where['phone'] = "";
            }

        }
        //die;


        $where = array_filter($where);
        $where["relation"]=2;
        // dump($whereroom);dump($where);exit();
        $page = request()->has('page') ? request()->get('page') : 1;
        //查询房屋条数
        $number = (new roomService())->personnelListNumber($where,$community_id);
        View::assign('number',$number);
        //查询房屋信息
        $personnellist = (new roomService())->personnelList($page,$where,$community_id);

        foreach($personnellist as $userinfo){
            $userinfo['room_id'] = $userinfo['building'].'幢'.$userinfo['unit'].'单元'.$userinfo['room'].'室';
            if($userinfo["personGuid"]=="0")
            {


                $cardNo=$userinfo["room"].substr($userinfo["room"],0,2);
                $type=$userinfo["community_name"]."-".$userinfo["address"];
                $re=(new wotuService())->reg($userinfo["name"],$userinfo["phone"],$cardNo,$userinfo["idcard"],$type);

                $result=json_decode($re,true);
                $guid=$result["data"]["guid"];
                Db::table("personnel")->where("id",$userinfo["id"])->update(["personGuid"=>$guid]);
                $userinfo["personGuid"]=$guid;
            }
            //获取uface中的图片
            $res=(new wotuService())->personGuid($userinfo["personGuid"]);
            //echo "<pre>";var_dump($res);die;
            $userinfo["pic"]=$res["data"]["faces"];
            $userinfo["picnum"]=count($userinfo["pic"]);
            $list[] = $userinfo;
        }

        if(!isset($list)){
            View::assign('list',$personnellist);
        }else{
            View::assign('list',$list);
        }
        View::assign('personnellist',$personnellist);

        foreach($data as $k => $v) {
            View::assign($k, $v);
        }
        return View::fetch('ternet_list');
    }
    //公司企业员工添加
    public function ternetadd(){
          //查询小区信息
        $com = $this->pidTokenId();
        //$whe['id'] = array('in',$com['community_id']);
        $whe=[];
        $whe[] =["id","in",$com['community_id']];
        $communitylist = (new roomService())->communityList($whe);
        $community_id = $com['community_id'];
        View::assign('communitylist',$communitylist);
        
        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();
            if(!$this->checkPhoneNumber($data["phone"])){
                $this->error('手机号码错误',url('ternetadd'));
            }

            $communityinfo=Db::table("community")->where("id",$data["community_id"])->find();
            
            $roominfo = (new roomService())->roominfoName($data["community_id"],$data["group"],$data["building"],$data["unit"],$data["room"]);
            $data['room_id'] = $roominfo['id'];
             $data["address"]=$data["group"].$data["building"]."幢".$data["unit"]."单元".$data["room"]."室";
            $data['community_name'] = $communityinfo["name"];
            $data['xmcode'] = $communityinfo["xmcode"];
            //添加时间
            $bill['create_time'] =time();
            //数据插入到数据库
            $result = Db::table("personnel")->insert($data);
            //判断数据是否插入成功
            if($result){
                $this->success('添加成功',url('ternetlist'));
            }else{
                $this->error('添加失败',url('ternetlist'));
            }
        }else{
           return View::fetch('ternet_add'); 
        }
    }


    //企业员工
    public function staffcompany()
    {

          //查询小区信息
        $com = $this->pidTokenId();
        //$whe['id'] = array('in',$com['community_id']);
        $whe=[];
        $whe[] =["id","in",$com['community_id']];
        $communitylist = (new roomService())->communityList($whe);
        $community_id = $com['community_id'];
        //多条件查询
        $data = request()->get();
        
        
       
        $where = [];
        $where['relation'] = 4;
        if(!empty($data)){
            
            if(!empty($data['name'])){
                $where['name'] = $data['name'];
            }else{
                $where['name'] = "";
            }
            if(!empty($data['phone'])){
                $where['phone'] = $data['phone'];
            }else{
                $where['phone'] = "";
            }
            if(!empty($data['department'])){
                $where['department'] = $data['department'];
            }else{
                $where['department'] = "";
            }
            if(!empty($data['address'])){
                $where['address'] = $data['address'];
            }else{
                $where['address'] = "";
            }
              
        }
       
        $where = array_filter($where);
        // dump($whereroom);dump($where);exit();
        $page = request()->has('page') ? request()->get('page') : 1;
        //查询房屋条数
        $number = (new roomService())->personnelListNumber($where,$community_id);
        View::assign('number',$number);
        //查询房屋信息
        $personnellist = (new roomService())->personnelList($page,$where,$community_id);
        foreach($personnellist as $userinfo){
            
            if($userinfo["personGuid"]=="0")
            {

                $cardNo=$userinfo["room"].substr($userinfo["room"],0,2);
                $type=$userinfo["community_name"]."-".$userinfo["address"];
                $re=(new wotuService())->reg($userinfo["name"],$userinfo["phone"],$cardNo,$userinfo["idcard"],$type);
                $result=json_decode($re,true);
                $guid=$result["data"]["guid"];
                Db::table("personnel")->where("id",$userinfo["id"])->update(["personGuid"=>$guid]);
                $userinfo["personGuid"]=$guid;
            }
            //获取uface中的图片
            $res=(new wotuService())->personGuid($userinfo["personGuid"]);

            //var_dump($res);die;
            $userinfo["pic"]=$res["data"]["faces"];
            $userinfo["picnum"]=count($userinfo["pic"]);

            $list[] = $userinfo;
        }
        /*if(!empty($number)){
            foreach ($personnellist as $key => $value) {
                $value["mzt"]=0;
                if($value["ryid"]!="0"){
                    $res=(new iotmcuService())->getJkmztByRyid($value["xmcode"],$value["ryid"],$value["idcard"],$value["relation"]);
                    
                    $re=json_decode($res,true);
                    if($re["code"]=="0"){
                        $value["mzt"]=$re["data"]["mzt"];
                    }
                    
                    
                }
                $list[] = $value;
                View::assign('list',$list);
            }
            
        }else{
            View::assign('list',$personnellist);
        }*/
        View::assign('list',$personnellist);
        View::assign('personnellist',$personnellist);
        
        foreach($where as $k => $v) {
            View::assign($k, $v);
        }
        return View::fetch('staff_company');
    }
    //公司企业员工添加
    public function staffcompanyadd(){
          //查询小区信息
        $com = $this->pidTokenId();
        //$whe['id'] = array('in',$com['community_id']);
        $whe=[];
        $whe[] =["id","in",$com['community_id']];
        $communitylist = (new roomService())->communityList($whe);
        $community_id = $com['community_id'];
        View::assign('communitylist',$communitylist);
        
        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();
            $communityinfo=Db::table("community")->where("id",$data["community_id"])->find();
            $data['community_name'] = $communityinfo["name"];
            $data['xmcode'] = $communityinfo["xmcode"];
            //添加时间
            $bill['create_time'] =time();
            //数据插入到数据库
            $result = Db::table("personnel")->insert($data);
            //判断数据是否插入成功
            if($result){
                $this->success('添加成功',url('staffcompany'));
            }else{
                $this->error('添加失败',url('staffcompany'));
            }
        }else{
           return View::fetch('staff_company_add'); 
        }
    }
    //物业员工
    public function staffproperty()
    {
          //查询小区信息
        $com = $this->pidTokenId();
        //$whe['id'] = array('in',$com['community_id']);
        $whe=[];
        $whe[] =["id","in",$com['community_id']];
        $communitylist = (new roomService())->communityList($whe);
        $community_id = $com['community_id'];
        View::assign('communitylist',$communitylist);
        //多条件查询
        $data = request()->get();
        
       
        $where = [];
        $where['relation'] = 5;  
        if(!empty($data)){
            
            if(!empty($data['name'])){
                $where['name'] = $data['name'];
            }else{
                $where['name'] = "";
            }
            if(!empty($data['phone'])){
                $where['phone'] = $data['phone'];
            }else{
                $where['phone'] = "";
            }
            if(!empty($data['department'])){
                $where['department'] = $data['department'];
            }else{
                $where['department'] = "";
            }
            if(!empty($data['address'])){
                $where['address'] = $data['address'];
            }else{
                $where['address'] = "";
            }
            
        }
        //die;
        
        $where = array_filter($where);
        // dump($whereroom);dump($where);exit();
        $page = request()->has('page') ? request()->get('page') : 1;
        //查询房屋条数
        $number = (new roomService())->personnelListNumber($where,$community_id);
        View::assign('number',$number);
        //查询房屋信息
        $personnellist = (new roomService())->personnelList($page,$where,$community_id);
        foreach($personnellist as $userinfo){
            
            if($userinfo["personGuid"]=="0")
            {

                $cardNo="222299";
                $type=$userinfo["community_name"]."-".$userinfo["address"];
                $re=(new wotuService())->reg($userinfo["name"],$userinfo["phone"],$cardNo,$userinfo["idcard"],$type);

                $result=json_decode($re,true);
                $guid=$result["data"]["guid"];
                Db::table("personnel")->where("id",$userinfo["id"])->update(["personGuid"=>$guid]);
                $userinfo["personGuid"]=$guid;
            }
            $res=(new wotuService())->personGuid($userinfo["personGuid"]);

            //var_dump($res);die;
            $userinfo["pic"]=$res["data"]["faces"];
            $userinfo["picnum"]=count($userinfo["pic"]);
            $list[] = $userinfo;
        }
        if(!isset($list)){
            View::assign('list',$personnellist);
        }else{
            View::assign('list',$list);
        }
        View::assign('personnellist',$personnellist);
        
        foreach($where as $k => $v) {
            View::assign($k, $v);
        }
        return View::fetch('staff_property');
    }
     //公司企业员工添加
    public function staffpropertyadd(){
          //查询小区信息
        $com = $this->pidTokenId();
        //$whe['id'] = array('in',$com['community_id']);
        $whe=[];
        $whe[] =["id","in",$com['community_id']];
        $communitylist = (new roomService())->communityList($whe);
        $community_id = $com['community_id'];
        View::assign('communitylist',$communitylist);
        
        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();
            $communityinfo=Db::table("community")->where("id",$data["community_id"])->find();
            
            
            $data['community_name'] = $communityinfo["name"];
            $data['xmcode'] = $communityinfo["xmcode"];
            //添加时间
            $bill['create_time'] =time();
            //数据插入到数据库
            $result = Db::table("personnel")->insert($data);
            //判断数据是否插入成功
            if($result){
                $this->success('添加成功',url('staffproperty'));
            }else{
                $this->error('添加失败',url('staffproperty'));
            }
        }else{
           return View::fetch('staff_property_add'); 
        }
    }
    

    public function ufaceadd(){
        if(request()->isPost()){
            $data=request()->post();
            $data["create_type"]=2;
            //查询房屋id
            $roominfo=(new personService())->roominfo($data["building"],$data["unit"],$data["room"]);
            if(empty($roominfo)){
                $this->error("该房屋不存在");
            }
            //查询该房屋业主是否已经绑定
            $ownerinfo=(new personService())->ownerinfo($roominfo["id"]);
            if($data["relation"]=="1"){
                if(!empty($ownerinfo)){
                    $this->error("该房屋下的业主已经添加");
                }
                
            }else{
                if(!empty($ownerinfo)){
                    $data["parent_user_id"]=$ownerinfo["user_id"];
                } 
            }
            $data["room_id"]=$roominfo["id"];
            $data["create_time"]=time();
            $id=(new personService())->personAdd($data);
            if($id){
                $url=url("ufaceEdit")."?id=".$id;
                echo "<script>location.href='$url'</script>";
                exit;
            }else{

            }
        }else{
            $buildings=Db::table("room")->distinct("buildings")->column("building");
            View::assign("buildings",$buildings);
            return View::fetch();
        }
       
    }
    //删除图片
    public function delimage(){
        $guid=request()->post("photoid");
        $personGuid=request()->post("personGuid");
        $result=(new wotuService())->delimage($personGuid,$guid);
        return $result;

    }

    public function test(){
        $userinfo["personGuid"]="AF485DAE5C1D4A2D97C3CFA65C237DA9";
        $result=(new wotuService())->del($userinfo["personGuid"]);
        var_dump($result);
    }
    //删除用户 
    public function ufacedel(){
        $id=request()->get("id");
        
        $userinfo=Db::table("personnel")->where("id",$id)->find();
        if(empty($userinfo)){
            return json(['errMsg' => '该用户不存在']);
        }
        $userinfo["del_userid"]=session("name");
        $res=Db::table("personnel_del")->insert($userinfo);
        if($res){
        	if($userinfo["ryid"]!="0"){
        		(new iotmcuService())->sfzx($userinfo["xmcode"],$userinfo["ryid"]);
        	}
        	if($userinfo["personGuid"]!="0"){
                $result=(new wotuService())->del($userinfo["personGuid"]);
                
        	}
            Db::table("personnel")->where("id",$id)->delete();
            $this->success("用户删除成功");
            return json(["errCode"=>"0",'msg' => '用户删除成功']);
        }else{
            return json(['msg' => '用户删除失败3']);
        }
    }
    
    public function personCheck()
    {
          //查询小区信息
        $com = $this->pidTokenId();
        //$whe['id'] = array('in',$com['community_id']);
        $whe=[];
        $whe[] =["id","in",$com['community_id']];
        $communitylist = (new roomService())->communityList($whe);
        //Log::write($communitylist,"community");
        $community_id = $com['community_id'];
        View::assign('communitylist',$communitylist);
        //查询
        $data = request()->get();
        // dump($data);exit();
        if (!empty($data['community_id'])){
            $groups = (new roomService())->disgroup($data['community_id']);
            View::assign('groups', $groups);
            // dump($data['group']);
            if (!empty($data['group'])) {
                $buildings = (new roomService())->disbuilding($data['community_id'],$data['group']);
                View::assign('buildings', $buildings);
                // dump($buildings);exit();
                if (!empty($data['building'])) {
                    $units =  (new roomService())->disunit($data['community_id'], $data['group'],$data['building']);
                    View::assign('units', $units);
                    if(!empty($data['unit'])){
                        $rooms = (new roomService())->disroom($data['community_id'], $data['group'],$data['building'],$data['unit']);
                        View::assign('rooms',$rooms);
                    }
                }
            }
        }
        
        $where = [];
        if(!empty($data)){
            if(!empty($data['community_id'])){
                $where['community_id'] = $data['community_id'];//小区名称
            }else{
                $where['community_id'] = "";
            }
            if(!empty($data['group'])){
                $where['group'] = $data['group'];//组团
            }else{
                $where['group'] = "";
            }
            if(!empty($data['building'])){
                $where['building'] = $data['building'];
            }else{
                $where['building'] = "";
            }
            if(!empty($data['unit'])){
                $where['unit'] = $data['unit'];
            }else{
                $where['unit'] = "";
            }
            if(!empty($data['room'])){
                $where['room'] = $data['room'];
            }else{
                $where['room'] = "";
            }
            if(!empty($data['owner_name'])){
                $where['name'] = $data['owner_name'];
            }else{
                $where['name'] = "";
            }
            if(!empty($data['phone'])){
                $where['phone'] = $data['phone'];
            }else{
                $where['phone'] = "";
            }
            
        }
       
        $where = array_filter($where);
        // dump($whereroom);dump($where);exit();
        $page = request()->has('page') ? request()->get('page') : 1;
        //查询房屋条数
        $number = (new roomService())->personcheckListNumber($where,$community_id);
        
        View::assign('number',$number);
        //查询房屋信息
        $personlist = (new roomService())->personcheckList($page,$where,$community_id);
        View::assign('list',$personlist);
        View::assign('personnellist',$personlist);
       
        foreach($where as $k => $v) {
            View::assign($k, $v);
        }
        return View::fetch('person_check');
    }
    
    public function checksuccess(){
        $id=request()->get("id");
        $info=Db::table("person_check")->where("id",$id)->find();
        if(empty($info)){
            $this->error("用户信息不存在");
        }
        //查询该房屋业主是否已经绑定
        /*$personinfo=Db::table("personnel")
                        ->where("room_id",$info["room_id"])
                        ->where("relation","0")
                        ->find();
        if(!empty($personinfo)){
            if($personinfo["user_id"]!="0"){
                $this->error("该房间业主已经绑定");
            }else{
                $this->error("该房间业主已经绑定");
            }
        }*/
        /*$send = send_check_success($info["phone"]);
        if($send['Message'] == 'OK'){
            $data["to_phone"]=$info["phone"];
            $data["type"]=1;
            $data["room_id"]=$info["room_id"];
            $data["send_time"]=time();
            Db::table("msg_log")->insert($data);
        }*/
        unset($info["img"]);
        unset($info["status"]);
        unset($info["id"]);
        unset($info["check_time"]);
        unset($info["check_name"]);
        $info["create_time"]=time();
        $info["db_parent_user_id"]=$info["user_id"];
        unset($info["user_id"]);
        Db::table("personnel")->insert($info);
        $res=Db::table("person_check")->where("id",$id)->update(["status"=>"1","check_time"=>time()]);
        $url=url("personcheck");
        echo "<script>location.href='$url'</script>";
        exit;
    }

    public function checkfail(){
        $id=request()->get("id");
        $info=Db::table("person_check")->where("id",$id)->find();
        if(empty($info)){
            $this->error("用户信息不存在");
        }
        
        /*$send = send_check_fail($info["phone"]);
        if($send['Message'] == 'OK'){
            $data["to_phone"]=$info["phone"];
            $data["type"]=2;
            $data["room_id"]=$info["room_id"];
            $data["send_time"]=time();
            Db::table("msg_log")->insert($data);
        }*/
        $res=Db::table("person_check")->where("id",$id)->update(["status"=>"3","check_time"=>time()]);
        $url=url("personcheck");
        echo "<script>location.href='$url'</script>";
        exit;
    }
    
    public function devicecancelaccredit(){
        if(request()->post()){
            $personGuid=request()->post("personGuid");
            $deviceKeys=request()->post("devicekey");
            $devicearr=explode(",",$deviceKeys);
            foreach ($devicearr as $deviceKey){
                $res=(new wotuService())->cancelaccredit($personGuid,$deviceKey);
            }
            return json(["code"=>0]);
        }
    }
    public function deviceaccredit(){ 
        if(request()->post()){
            $personGuid=request()->post("personGuid");
            $deviceKeys=request()->post("devicekey");
            $devicearr=explode(",",$deviceKeys);
            foreach ($devicearr as $deviceKey){
                $res=(new wotuService())->accredit($personGuid,$deviceKey);
            }
            return json(["code"=>0]);
        }else{
            $id=request()->get("id");

            $userinfo=Db::table("personnel")->where("id",$id)->find();
            if(empty($userinfo)){
                return "该用户不存在";
            }
            if($userinfo["personGuid"]=="0"){
                return "该用户错误";
            }
            View::assign("userinfo",$userinfo);
            //查询该用户已经授权的设备
            $result=(new wotuService())->devicelist($userinfo["personGuid"]);
            //echo "<pre>";var_dump($result);die;
            if($result["code"]!="WO_SUS1000"){

                echo "<pre>";var_dump($result);die;
            }
            $data=$result["data"]["content"];
            $devices=array();
            foreach ($data as $key => $value) {
                $devices[]=$value["deviceKey"];
            }
            View::assign("devices",$devices);
            //echo "<pre>";var_dump($devices);die;
            $devicelist=Db::table("device")->where("community_id",$userinfo["community_id"])->select();
            //查询所有的设备
            foreach ($devicelist as $key => $value) {
                $value["state"]="";
                $value["onlineState"]="";
                $res=(new wotuService())->deviceinquiry($value["devicekey"]);
                $rest=json_decode($res, true);
                //echo "<pre>";var_dump($rest);die;
                if($rest["code"]=="WO_SUS1000"){

                    $value["state"]=$rest["data"]["state"];
                    $value["onlineState"]=$rest["data"]["onlineState"];

                }

                //var_dump($value);
                $list[] = $value;
            }
            if(!isset($list)){
                View::assign('devicelist',$devicelist);
            }else{
                View::assign('devicelist',$list);
            }
            return View::fetch("device_accredit");
        }
        
    }
    
    
    public function personnelEdit(){
		$id=request()->get("id");
		$userinfo=Db::table("personnel")->where("id",$id)->find();
		if(empty($userinfo)){
            return json(['errMsg' => '该用户不存在']);
        }
        if($userinfo["personGuid"]=="0"||empty($userinfo["personGuid"])){
            $address=$userinfo["building"]."幢".$userinfo["unit"]."单元".$userinfo["room"]."室";
            $type=$userinfo["relation"];         

            $re=(new wotuService())->reg($userinfo["name"],$userinfo["phone"],$userinfo["ryid"],$userinfo["idcard"],$type);
            $result=json_decode($re,true);
            $guid=$result["data"]["guid"];
            Db::table("personnel")->where("id",$userinfo["id"])->update(["personGuid"=>$guid]);
            $userinfo["personGuid"]=$guid;
        }
        $userinfo["idNo"]="";
        $userinfo["idcardNo"]="";
        //获取uface中的用户信息
        $ufaceinfo=(new wotuService())->personGuid($userinfo["personGuid"]);
        //echo "<pre>";var_dump($ufaceinfo);die;
        if($ufaceinfo["code"]!="WO_SUS1000"){
            return "用户查询失败";
        }
        $userinfo["cardNo"]=$ufaceinfo["data"]["cardNo"];
        $userinfo["idCardNo"]=$ufaceinfo["data"]["idCardNo"];
        $userinfo["faces"]=$ufaceinfo["data"]["faces"];

        $num=count( $userinfo["faces"]);
        View::assign("num",$num);
        View::assign("data",$userinfo["faces"]);
        View::assign("userinfo",$userinfo);
       	return View::fetch("personnel_edit");
	}
	
	public function idcardEdit(){
	    if(request()->post()){
	        $data=request()->post();
	        $res=Db::table("personnel")->update($data);
	        if($res){
	            $this->success("修改成功");
	        }else{
	            $this->error("修改失败");
	        }
	        
	    }else{
	        $id=request()->get("id");
    		$userinfo=Db::table("personnel")->where("id",$id)->find();
    		if(empty($userinfo)){
                return json(['errMsg' => '该用户不存在']);
            }
            if($userinfo["ryid"]!='0'){
                return "健康码已申请，无法修改";
                
            }
            View::assign("userinfo",$userinfo);
            
           	return View::fetch("idcard_edit");
	    }
		
	}
    public function cam(){
        $id=request()->get("id");
        $userinfo=Db::table("personnel")->where("id",$id)->find();
        if($userinfo["personGuid"]=="0"||empty($userinfo["personGuid"]))
        {
            $cardNo=$userinfo["room"].substr($userinfo["room"],0,2);
            $type=$userinfo["community_name"]."-".$userinfo["address"];
            $re=(new wotuService())->reg($userinfo["name"],$userinfo["phone"],$cardNo,$userinfo["idcard"],$type);

            $result=json_decode($re,true);
            if($result["code"] != "WO_SUS1000"){
                echo $result["msg"];
                exit;
            }
            $guid=$result["data"]["guid"];
            Db::table("personnel")->where("id",$userinfo["id"])->update(["personGuid"=>$guid]);
            $userinfo["personGuid"]=$guid;
        }

        $res=(new wotuService())->personGuid($userinfo["personGuid"]);

        $userinfo["pic"]=$res["data"]["faces"];

        $num=count($userinfo["pic"]);
        if($num==3){
            return "只能传三张图片";
        }
        View::assign("id",$id);
        return View::fetch("cam");
    }
    //拍照上传
    public function uploadimg()
    {
        $img=request()->post("img");
        $id=request()->post("id");
        //return $id;
        //获取uface中的图片
        $userinfo=Db::table("personnel")->where("id",$id)->find();
        $res=(new wotuService())->tupian($userinfo["personGuid"]);
        $userinfo["pic"]=$res["data"];
        //var_dump($userinfo["pic"]);die;
        $img=str_replace("data:image/png;base64,","",$img);
        $image=(new wotuService())->urlsafe_b64decode($img);
        
        $guid=$userinfo["personGuid"];
        $result=(new wotuService())->uploadimg($image,$guid);
        //var_dump($result);die;
        if($result["code"]=="WO_SUS1000"){
            //查询该小区下的人脸设备并授权
            $locklist=(new deviceService())->userdeviceListPid($id);
            $deviceKeys="";
            if(empty($locklist)){
                return json(["errCode"=>"0",'msg' =>"图片上传成功"]); 
            }
            foreach ($locklist as $key => $value) {
                (new wotuService())->accredit($guid, $value["devicekey"]);

            }

            return json(["errCode"=>"0",'msg' =>"图片上传成功"]);
        }else{
            return json(["errCode"=>"1",'errmsg' =>"图片上传失败"]);
        }
    }

    public function uploadimage(){
        if(request()->isPost()){
            $data=request()->post();

            $file = request()->file('myfile');

            if ($file) {
                $info = $file->move('ufaceupload');
                if($info){
                    $file = $info->getSaveName();
                    $file = str_replace('\\', '/', $file);
                    $image = "https://www.zhqjw.zfbzhsq.com/ufaceupload/".$file;
                    $userinfo=Db::table("personnel")->where("id",$data["id"])->find();
                    $res=(new wotuService())->uploadimgurl($image, $userinfo["personGuid"]);
                    //Log::write($res,"uploadimg");
                    if($res["code"]=="WO_SUS1000"){
                        //查询该小区下的人脸设备并授权
                        $locklist=(new deviceService())->userdeviceListPid($data["id"]);
                        $deviceKeys="";
                        if(empty($locklist)){
                            return "图片上传成功";
                            return json(["errCode"=>"0",'msg' =>"图片上传成功"]);
                        }
                        foreach ($locklist as $key => $value) {

                            $res=(new wotuService())->accredit($userinfo["personGuid"], $value["devicekey"]);
                        }

                        return "图片上传成功";
                        return json(["errCode"=>"0",'msg' =>"图片上传成功"]);
                    }else{
                        return "图片上传失败";
                        return json(["errCode"=>"1",'errmsg' =>"图片上传失败"]);
                    }
                }else{
                    return "图片上传失败";
                }
            }

        }else{
            $id=request()->get("id");
            View::assign("id",$id);
            return View::fetch();
        }

    }
    public function urlsafe_b64decode($string) {
       $data = str_replace(array('-','_'),array('+','/'),$string);
       $mod4 = strlen($data) % 4;
       if ($mod4) {
           $data .= substr('====', $mod4);
       }
       return $data;
       return base64_decode($data);
    }

    function checkPhoneNumber($phone_number)
    {

        //中国联通号码：130、131、132、145（无线上网卡）、155、156、185（iPhone5上市后开放）、186、176（4G号段）、175（2015年9月10日正式启用，暂只对北京、上海和广东投放办理）,166,146
        //中国移动号码：134、135、136、137、138、139、147（无线上网卡）、148、150、151、152、157、158、159、178、182、183、184、187、188、198
        //中国电信号码：133、153、180、181、189、177、173、149、199
        $g = "/^1[34578]\d{9}$/";
        $g2 = "/^19[89]\d{8}$/";
        $g3 = "/^166\d{8}$/";
        if (preg_match($g, $phone_number)) {
            return true;
        } else if (preg_match($g2, $phone_number)) {
            return true;
        } else if (preg_match($g3, $phone_number)) {
            return true;
        }

        return false;
    }

}